const configs = require("./src/configs/index.js");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const time = new Date().getTime();
const fs = require('fs')
const path = require('path')
let buildProject = process.env.VUE_APP_BuildProject || 'main'
const resolve = (dir) => {
  return path.join(__dirname, dir)
}
let getAlias = function () {
  let alias = {}
  const files = fs.readdirSync('src/project/')
  files.forEach((dirName) => {
    alias[`@${dirName}`] = resolve(`src/project/${dirName}`)
  })
  return alias
}
console.log("路径", {
  '@': resolve('src'),
  ...getAlias(),
});

module.exports = {
  outputDir: "oper", // 编译生成文件夹名称
  devServer: {
    open: true, // 启动服务自动打开浏览器
    proxy: {
      "/host": {
        target: "http://192.168.1.102:8080",
        changeOrigin: true,//表示在请求头中的 Origin 标头会被更改为目标服务器的地址，以便确保跨域请求正常工作。
        pathRewrite: {
          "/host": "/",
        },
      },
    },
  },
  publicPath: configs.BASE_URL, // 项目根目录和主域名之间的路径
  css: {
    loaderOptions: {
      sass: {
        prependData: `
          @import "@/assets/css/index.scss";
        `,
      },
    },
  },
  productionSourceMap: false,
  configureWebpack: {
    plugins: [
      new CopyWebpackPlugin([
        {
          from: "node_modules/@liveqing/liveplayer/dist/component/crossdomain.xml",
        },
        {
          from: "node_modules/@liveqing/liveplayer/dist/component/liveplayer.swf",
        },
        {
          from: "node_modules/@liveqing/liveplayer/dist/component/liveplayer-lib.min.js",
          to: "js/",
        },
      ]),
    ],
    output: {
      filename: `[name].${time}.js`,
      chunkFilename: `[name].${time}.js`,
    },
    resolve: {
      alias: {
        '@': resolve('src'),
        ...getAlias(),
      },
    },
  },
  pages: {
    buildProject: {
      // page 的入口
      entry: `./src/project/${buildProject}/main`,
      // 模板来源
      template: `./public/${buildProject}/index.html`,
      // 在 dist/index.html 的输出
      filename: `index.html`,
      // 当使用 faviconPath 选项时，
      // template 中的 icon 标签需要是 <title><%= htmlWebpackPlugin.options.faviconPath %></title>
      faviconPath: '',
      // 当使用 title 选项时，
      // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
      title: '',
      // 在这个页面中包含的块，默认情况下会包含
      // 提取出来的通用 chunk 和 vendor chunk。
      // chunks: ['chunk-vendors', 'chunk-common', buildProject],
    },
  }
};
